﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;

namespace Parichay.Domain.Models
{
    public class FileUploadModel
    {
        public string FileName { get; set; }
        public int ContentLength { get; set; }
        public string ContentType { get; set; }
        public Guid Guid { get; set; }
        public string FileExtension { get; set; }
        public bool IsSuccess { get; set; }
        public string StatusMessage { get; set; }

        public FileUploadModel(HttpPostedFileBase file)
        {
            FileName = file.FileName;
            ContentLength = file.ContentLength;
            ContentType = file.ContentType;
            FileExtension = Path.GetExtension(file.FileName);
            Guid = Guid.NewGuid();
            IsSuccess = true;
            StatusMessage = "";
        }

        public FileUploadModel(string fileDescription)
        {
            string[] arrSplit = fileDescription.Split(new char[] { '|' });
            ContentType = arrSplit[0];
            ContentLength = int.Parse(arrSplit[1]);
            FileName = arrSplit[2];
            Guid = Guid.Parse(arrSplit[3]);
            FileExtension = Path.GetExtension(FileName);
            IsSuccess = true;
            StatusMessage = "";
        }

        public FileUploadModel()
        {
            // TODO: Complete member initialization
        }

        public override string ToString()
        {
            return (ContentType + "|" + ContentLength.ToString() + "|" + FileName + "|" + Guid.ToString());
        }

        public string getAbsoluteLocation()
        {
            string folder = Utilities.getConfigSetting("UploadFileLocation", "/Uploads/");
            folder = (string.IsNullOrEmpty(folder)) ? Constants.UploadFileLocation : "/Uploads/";
            return (HttpContext.Current.Server.MapPath(folder));
        }

        public string getAbosoluteFilePath()
        {
            return (Path.Combine(getAbsoluteLocation(), Path.GetFileName(Guid.ToString())));
        }

        public void deleteFile()
        {
            System.IO.File.Delete(this.getAbosoluteFilePath());
        }

        public bool writeBytesToFileLocation(byte[] buffer)
        {
            if ((buffer == null) || (buffer.Length == 0))
            {
                IsSuccess = false;
                StatusMessage = "Error: Buffer is Empty.";
                return false;
            }
            string path = System.IO.Path.Combine(this.getAbosoluteFilePath());

            if (System.IO.File.Exists(path))
            {
                IsSuccess = false;
                StatusMessage = "Error: File already exists with same Name(GUID). Please try again later.";
                return false;
            }

            if (!System.IO.Directory.Exists(this.getAbsoluteLocation()))
            {
                System.IO.Directory.CreateDirectory(this.getAbsoluteLocation());
            }

            System.IO.File.WriteAllBytes(this.getAbosoluteFilePath(), buffer);

            this.StatusMessage = "Success: File Location:[" + this.getAbsoluteLocation() + "] Details: [" + this.ToString() + "]";
            this.IsSuccess = true;
            return true;
        }

        private enum AttachmentType { unknown = 0, xls, xlsx, ppt, pptx, pdf, doc, docx, txt, htm }
        private int GetContentType(string fileNameWithExtension)
        {
            string[] tmp = fileNameWithExtension.Split('.');
            string fileExtension = tmp[tmp.Length - 1].ToLower();
            AttachmentType result = AttachmentType.unknown;
            if (Enum.TryParse<AttachmentType>(fileExtension, out result))
                return (int)result;
            else
                return (int)AttachmentType.unknown;
        }
    }
}
